{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import keras\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras.models import Sequential, Model\n",
    "from keras.layers import Input, Dense, Dropout, BatchNormalization\n",
    "from keras.layers.advanced_activations import LeakyReLU\n",
    "from keras.optimizers import Adam\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.utils import shuffle\n",
    "from mysql import SQLConnector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_discriminator(layer1, layer2, layer3, alpha):\n",
    "    model = Sequential()\n",
    "    model.add(Dense(layer1, input_dim=41)) #discriminator takes 41 values from our dataset\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dropout(0.3))\n",
    "    model.add(Dense(layer2))\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dropout(0.3))\n",
    "    model.add(Dense(layer3))\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dropout(0.3))\n",
    "    model.add(Dense(1, activation='sigmoid')) #outputs 0 to 1, 1 being real and 0 being fake\n",
    "\n",
    "    attack = Input(shape=(41,))\n",
    "    validity = model(attack)\n",
    "\n",
    "    return Model(attack, validity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_generator(layer1, layer2, layer3, alpha):\n",
    "    model = Sequential()\n",
    "    model.add(Dense(layer1, input_dim=41))\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dense(layer2))\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dense(layer3))\n",
    "    model.add(BatchNormalization())\n",
    "    model.add(LeakyReLU(alpha=alpha))\n",
    "    model.add(Dense(41, activation='relu'))\n",
    "\n",
    "    noise = Input(shape=(41,))\n",
    "    attack = model(noise)\n",
    "    return Model(noise, attack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GAN_model(layer1, layer2, layer3, alpha):\n",
    "    optimizer = Adam(0.001)\n",
    "    \n",
    "    #build generator and discriminator (mirrored)\n",
    "    generator = build_generator(layer1, layer2, layer3, alpha)\n",
    "    \n",
    "    discriminator = build_discriminator(layer3, layer2, layer1, alpha)\n",
    "    discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n",
    "    \n",
    "    #input and output of our combined model\n",
    "    z = Input(shape=(41,))\n",
    "    attack = generator(z)\n",
    "    validity = discriminator(attack)\n",
    "    \n",
    "    #build combined model from generator and discriminator\n",
    "    combined = Model(z, validity)\n",
    "    combined.compile(loss='binary_crossentropy', optimizer=optimizer)\n",
    "    return combined, discriminator, generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "ll = [\"duration\", \"protocol_type\", \"service\", \"flag\", \"src_bytes\", \"dst_bytes\", \"land\", \"wrong_fragment\", \"urgent\", \n",
    "            \"hot\", \"num_failed_logins\", \"logged_in\", \"num_compromised\", \"root_shell\", \"su_attempted\", \"num_root\", \"num_file_creations\", \n",
    "            \"num_shells\", \"num_access_files\", \"num_outbound_cmds\", \"is_host_login\", \"is_guest_login\", \"count\", \"srv_count\", \"serror_rate\", \n",
    "            \"srv_serror_rate\", \"rerror_rate\", \"srv_rerror_rate\", \"same_srv_rate\", \"diff_srv_rate\", \"srv_diff_host_rate\", \"dst_host_count\", \n",
    "            \"dst_host_srv_count\", \"dst_host_same_srv_rate\", \"dst_host_diff_srv_rate\", \"dst_host_same_src_port_rate\", \n",
    "            \"dst_host_srv_diff_host_rate\", \"dst_host_serror_rate\", \"dst_host_srv_serror_rate\", \"dst_host_rerror_rate\", \n",
    "            \"dst_host_srv_rerror_rate\", \"attack_type\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_loop(combined, discriminator, generator, estimator, epochs):\n",
    "    epochs = epochs+1\n",
    "    batch_size = 30\n",
    "    conn = SQLConnector()\n",
    "    data = conn.pull_kdd99(attack='back', num=968)\n",
    "    dataframe = pd.DataFrame.from_records(data=data,\n",
    "                columns=conn.pull_kdd99_columns(allQ=True))\n",
    "    \n",
    "    #apply \"le.fit_transform\" to every column (usually only works on 1 column)\n",
    "    le = LabelEncoder()\n",
    "    dataframe_encoded = dataframe.apply(le.fit_transform)\n",
    "    dataset = dataframe_encoded.values\n",
    "    \n",
    "    f = open(\"backReal.txt\", \"a\")\n",
    "    np.savetxt(\"backReal.txt\", dataset, fmt=\"%d\")\n",
    "    f.close()\n",
    "    \n",
    "    #labels for data. 1 for valid attacks, 0 for fake (generated) attacks\n",
    "    valid = np.ones((batch_size, 1))\n",
    "    fake = np.zeros((batch_size, 1))\n",
    "    \n",
    "    #Set X as our input data and Y as our label\n",
    "    X_train = dataset[:, 0:41].astype(int)\n",
    "    Y_train = dataset[:, 41]\n",
    "    \n",
    "    #break condition for training (when diverging)\n",
    "    loss_increase_count = 0\n",
    "    prev_g_loss = 0\n",
    "    \n",
    "    #generating a np array of numbers 0..batch_size-1\n",
    "    idx = np.arange(batch_size)\n",
    "    \n",
    "    for epoch in range(epochs):\n",
    "        #selecting batch_size random attacks from our training data\n",
    "        #idx = np.random.randint(0, X_train.shape[0], batch_size)\n",
    "        attacks = X_train[idx-1]\n",
    "        \n",
    "        #generate a matrix of noise vectors\n",
    "        noise = np.random.normal(0, 1, (batch_size, 41))\n",
    "        \n",
    "        #create an array of generated attacks\n",
    "        gen_attacks = generator.predict(noise)\n",
    "        \n",
    "        #loss functions, based on what metrics we specify at model compile time\n",
    "        d_loss_real = discriminator.train_on_batch(attacks, valid)\n",
    "        d_loss_fake = discriminator.train_on_batch(gen_attacks, fake)\n",
    "        d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)\n",
    "        \n",
    "        #generator loss function\n",
    "        g_loss = combined.train_on_batch(noise, valid)\n",
    "        \n",
    "        if epoch % 50 == 0:\n",
    "            print(\"%d [D loss: %f, acc.: %.2f%%] [G loss: %f] [Loss change: %.3f, Loss increases: %.0f]\" % \n",
    "                  (epoch, d_loss[0], 100 * d_loss[1], g_loss, g_loss - prev_g_loss, loss_increase_count))\n",
    "        \n",
    "            #saving results to txt to track them as the gan is training\n",
    "            f = open(\"back.txt\", \"a\")\n",
    "            np.savetxt(\"back.txt\", gen_attacks, fmt=\"%d\")\n",
    "            f.close()\n",
    "            \n",
    "            y_pred = estimator.predict(gen_attacks)\n",
    "        \n",
    "            right = (y_pred >= 0.98).sum()\n",
    "            wrong = len(y_pred)-(y_pred >= 0.98).sum()\n",
    "            accuracy = (right/float(right+wrong))\n",
    "            print(\"Number of right predictions: %d\" % right)\n",
    "            print(\"Number of wrong predictions: %d\" % wrong)\n",
    "            print(\"Accuracy: %.4f \" % accuracy)      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 ... 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "conn = SQLConnector()\n",
    "data = conn.pull_kdd99(attack='back', num=968)\n",
    "data += conn.pull_kdd99(attack='normal', num=968)\n",
    "dataframe = pd.DataFrame.from_records(data=data,\n",
    "            columns=conn.pull_kdd99_columns(allQ=True))\n",
    "\n",
    "#LabelEncoder, turns all our categorical data into integers\n",
    "le = LabelEncoder()\n",
    "\n",
    "dataframe_encoded = dataframe.apply(le.fit_transform)\n",
    "dataset = dataframe_encoded.values\n",
    "\n",
    "#Set X as our input data and Y as our label\n",
    "X = dataset[:,0:41].astype(int)\n",
    "Y = dataset[:,41]\n",
    "print(Y)\n",
    "X, Y = shuffle(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Get validation data\n",
    "validationToTrainRatio = 0.10\n",
    "validationSize = int(validationToTrainRatio * len(X))\n",
    "validationData = X[:validationSize]\n",
    "validationLabels = Y[:validationSize]\n",
    "X = X[validationSize:]\n",
    "Y = Y[validationSize:]\n",
    "\n",
    "#Get test data\n",
    "testToTrainRatio = 0.10\n",
    "testSize = int(testToTrainRatio * len(X))\n",
    "testData = X[:testSize]\n",
    "testLabels = Y[:testSize]\n",
    "X = X[testSize:]\n",
    "Y = Y[testSize:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def baseline_model(layers, units, dropout_rate, input_shape, num_classes):\n",
    "    model = Sequential()\n",
    "    model.add(Dropout(rate=dropout_rate, input_shape=input_shape))\n",
    "    for _ in range(layers-1):\n",
    "        model.add(Dense(units=units, activation='relu'))\n",
    "        model.add(Dropout(rate=dropout_rate))\n",
    "\n",
    "    model.add(Dense(units=num_classes, activation='sigmoid'))\n",
    "    model.compile(optimizer=Adam(0.001),\n",
    "              loss='binary_crossentropy',\n",
    "              metrics=['accuracy'])\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/ldeng/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py:423: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From /home/ldeng/anaconda3/envs/ml/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
      "WARNING:tensorflow:From /home/ldeng/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "Train on 1569 samples, validate on 193 samples\n",
      "Epoch 1/200\n",
      " - 1s - loss: 6.5143 - acc: 0.5322 - val_loss: 2.1891 - val_acc: 0.8446\n",
      "Epoch 2/200\n",
      " - 0s - loss: 6.7403 - acc: 0.5233 - val_loss: 2.3471 - val_acc: 0.8187\n",
      "Epoch 3/200\n",
      " - 0s - loss: 6.6177 - acc: 0.5264 - val_loss: 3.1238 - val_acc: 0.7668\n"
     ]
    }
   ],
   "source": [
    "estimator = baseline_model(layers=2, units=32, dropout_rate=0.5, input_shape=X.shape[1:], num_classes=1)\n",
    "\n",
    "callbacks = [keras.callbacks.EarlyStopping(\n",
    "        monitor='val_loss', patience=2)]\n",
    "\n",
    "history = estimator.fit(X,\n",
    "                    Y,\n",
    "                    epochs=200,\n",
    "                    batch_size=256,\n",
    "                    callbacks=callbacks,\n",
    "                    validation_data=(validationData, validationLabels),\n",
    "                    verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "174/174 [==============================] - 0s 19us/step\n",
      "[2.571803575274588, 0.7816091960874098]\n"
     ]
    }
   ],
   "source": [
    "#Evalueating model on the testset\n",
    "#[loss, accuracy]\n",
    "print(estimator.evaluate(testData, testLabels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#creating GAN model\n",
    "combined, discriminator, generator = GAN_model(8, 16, 32, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [D loss: 1.014197, acc.: 53.33%] [G loss: 0.250629] [Loss change: 0.251, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "50 [D loss: 0.962994, acc.: 48.33%] [G loss: 0.275464] [Loss change: 0.275, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "100 [D loss: 0.999638, acc.: 45.00%] [G loss: 0.351420] [Loss change: 0.351, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "150 [D loss: 1.073447, acc.: 48.33%] [G loss: 0.271253] [Loss change: 0.271, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "200 [D loss: 0.885713, acc.: 51.67%] [G loss: 0.216677] [Loss change: 0.217, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "250 [D loss: 1.062767, acc.: 50.00%] [G loss: 0.286770] [Loss change: 0.287, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n",
      "300 [D loss: 0.984702, acc.: 51.67%] [G loss: 0.255285] [Loss change: 0.255, Loss increases: 0]\n",
      "Number of right predictions: 30\n",
      "Number of wrong predictions: 0\n",
      "Accuracy: 1.0000 \n"
     ]
    }
   ],
   "source": [
    "#training GAN model\n",
    "train_loop(combined, discriminator, generator, estimator, 300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999904]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.978531]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9985417]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.18840319]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.994249]\n",
      "[0.9999976]\n",
      "[0.99999917]\n",
      "[0.99760175]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99938214]\n",
      "[0.99999976]\n",
      "[1.]\n",
      "[0.21893069]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.15312201]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.66860336]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.16297367]\n",
      "[0.21546447]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.82340264]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9997105]\n",
      "[1.]\n",
      "[0.18174356]\n",
      "[1.]\n",
      "[0.93237096]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9998952]\n",
      "[0.9965172]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99779785]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999998]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9997877]\n",
      "[0.993406]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.51716167]\n",
      "[1.]\n",
      "[0.99994504]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999956]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.4063697]\n",
      "[1.]\n",
      "[0.21933219]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.83482987]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99926674]\n",
      "[0.99999976]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.70750886]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99967873]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9685322]\n",
      "[0.9998732]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999813]\n",
      "[0.99538517]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.83482987]\n",
      "[0.99999994]\n",
      "[0.9979681]\n",
      "[0.99974877]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.20834425]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9968413]\n",
      "[0.38656747]\n",
      "[0.9117907]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99723876]\n",
      "[1.]\n",
      "[0.9841225]\n",
      "[0.17322844]\n",
      "[1.]\n",
      "[0.5764773]\n",
      "[0.9997399]\n",
      "[0.8343265]\n",
      "[0.40359694]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99791706]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.7459253]\n",
      "[1.]\n",
      "[0.6739937]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99992967]\n",
      "[0.971719]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99978733]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99992496]\n",
      "[1.]\n",
      "[0.8249891]\n",
      "[0.9308721]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9251564]\n",
      "[0.8104561]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99459314]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99932635]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999982]\n",
      "[0.9999999]\n",
      "[0.998771]\n",
      "[0.99128723]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999765]\n",
      "[0.9999994]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[0.99999905]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.5863382]\n",
      "[0.97410977]\n",
      "[0.99910665]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9861855]\n",
      "[0.9999453]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999445]\n",
      "[1.]\n",
      "[0.999999]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99682105]\n",
      "[1.]\n",
      "[0.952723]\n",
      "[0.9997252]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.20993099]\n",
      "[0.9999069]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999962]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999821]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.720703]\n",
      "[0.9879838]\n",
      "[0.85804105]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.18698823]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999548]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9428139]\n",
      "[1.]\n",
      "[0.9999968]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9998932]\n",
      "[0.9999745]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999464]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.48376444]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9841225]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999998]\n",
      "[0.90601933]\n",
      "[0.9999996]\n",
      "[0.9753533]\n",
      "[1.]\n",
      "[0.99929094]\n",
      "[0.99987936]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9826257]\n",
      "[0.99242646]\n",
      "[1.]\n",
      "[0.16949281]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999996]\n",
      "[0.994249]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.84885335]\n",
      "[0.8784654]\n",
      "[1.]\n",
      "[0.9439822]\n",
      "[0.19806427]\n",
      "[1.]\n",
      "[0.41368443]\n",
      "[1.]\n",
      "[0.73305666]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.999756]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.999559]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99978733]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999987]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9996997]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9723935]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9995698]\n",
      "[0.9996171]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999946]\n",
      "[0.998191]\n",
      "[0.9899783]\n",
      "[1.]\n",
      "[0.9105965]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9989289]\n",
      "[0.506715]\n",
      "[1.]\n",
      "[0.93237096]\n",
      "[0.99957705]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.48376444]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999585]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999949]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999845]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[0.99987745]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.86454594]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99922425]\n",
      "[1.]\n",
      "[0.9690896]\n",
      "[1.]\n",
      "[0.99999946]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9117907]\n",
      "[1.]\n",
      "[0.9998967]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.19806427]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9997215]\n",
      "[1.]\n",
      "[0.896306]\n",
      "[0.19453982]\n",
      "[1.]\n",
      "[0.99779785]\n",
      "[0.999998]\n",
      "[0.15878731]\n",
      "[1.]\n",
      "[0.9999997]\n",
      "[0.17254013]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999993]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9998895]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9997886]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.5750473]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.98532104]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.97480196]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99730635]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999993]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9996436]\n",
      "[1.]\n",
      "[0.84933376]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.993791]\n",
      "[1.]\n",
      "[0.165759]\n",
      "[0.99999547]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.952723]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.49008134]\n",
      "[1.]\n",
      "[0.9990345]\n",
      "[0.999107]\n",
      "[0.9251564]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99863565]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[0.9662962]\n",
      "[1.]\n",
      "[0.999967]\n",
      "[1.]\n",
      "[0.99999166]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.4188038]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9393308]\n",
      "[1.]\n",
      "[0.9999999]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999739]\n",
      "[0.830606]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9505958]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99345934]\n",
      "[0.16449198]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999633]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.1632297]\n",
      "[1.]\n",
      "[0.99998045]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9963404]\n",
      "[1.]\n",
      "[0.9985417]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.999004]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999213]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.41338918]\n",
      "[0.39880264]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9251564]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.32113242]\n",
      "[0.9999981]\n",
      "[0.15135247]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.21307862]\n",
      "[1.]\n",
      "[0.8249891]\n",
      "[0.9999982]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.21326378]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999314]\n",
      "[0.4517492]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9976604]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.55687326]\n",
      "[1.]\n",
      "[0.15352944]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999547]\n",
      "[1.]\n",
      "[0.9895505]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999595]\n",
      "[1.]\n",
      "[0.835101]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999984]\n",
      "[1.]\n",
      "[0.7174568]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9899783]\n",
      "[1.]\n",
      "[0.9993609]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.3552949]\n",
      "[1.]\n",
      "[0.2403039]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.17016444]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.16499344]\n",
      "[0.99859]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99842894]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.99999976]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999962]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999993]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999982]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.8349329]\n",
      "[1.]\n",
      "[0.99760175]\n",
      "[0.99999976]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999974]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.940235]\n",
      "[1.]\n",
      "[0.15995625]\n",
      "[1.]\n",
      "[0.9642458]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.539417]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.8146925]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.62659395]\n",
      "[1.]\n",
      "[0.99884427]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.21247962]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.34950292]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.8401921]\n",
      "[1.]\n",
      "[0.99999994]\n",
      "[1.]\n",
      "[0.999998]\n",
      "[0.9895505]\n",
      "[0.31970203]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.16399357]\n",
      "[1.]\n",
      "[0.16259608]\n",
      "[1.]\n",
      "[0.9671828]\n",
      "[1.]\n",
      "[0.58876073]\n",
      "[1.]\n",
      "[0.59870493]\n",
      "[0.6918737]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.999992]\n",
      "[0.99978006]\n",
      "[1.]\n",
      "[1.]\n",
      "[0.9999691]\n",
      "[1.]\n",
      "Total 968, predicted attacks 858\n"
     ]
    }
   ],
   "source": [
    "conn = SQLConnector()\n",
    "data = conn.pull_kdd99(attack='back', num=968)\n",
    "dataframe = pd.DataFrame.from_records(data=data,\n",
    "            columns=conn.pull_kdd99_columns(allQ=True))\n",
    "le = LabelEncoder()\n",
    "dataframe_encoded = dataframe.apply(le.fit_transform)\n",
    "dataset = dataframe_encoded.values\n",
    "pred = estimator.predict(dataset[:, 0:41])\n",
    "counter = 0\n",
    "for x in pred:\n",
    "    print(x)\n",
    "    if x[0] >= 0.98:\n",
    "        counter += 1\n",
    "print('Total %d, predicted attacks %d' % (len(pred), counter))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mysql import SQLConnector\n",
    "conn = SQLConnector()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.99999845],\n",
       "       [1.        ],\n",
       "       [0.99999994],\n",
       "       [0.9997455 ],\n",
       "       [1.        ],\n",
       "       [0.9999991 ],\n",
       "       [0.999996  ],\n",
       "       [1.        ],\n",
       "       [1.        ],\n",
       "       [1.        ]], dtype=float32)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "noise = np.random.normal(0, 1, (10, 41)) #927\n",
    "\n",
    "#create an array of generated attacks\n",
    "gen_attacks = generator.predict(noise).astype('int')\n",
    "estimator.predict(gen_attacks)\n",
    "#data = conn.pull_kdd99(attack='nmap', num=8)\n",
    "for x in gen_attacks:\n",
    "    conn.write_gens('927', x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], \n",
    "                    x[11], x[12], x[13], x[14], x[15], x[16], x[17], x[18], x[19], x[20], \n",
    "                    x[21], x[22], x[23], x[24], x[25], x[26], x[27], x[28], x[29], x[30], \n",
    "                    x[31], x[32], x[33], x[34], x[35], x[36], x[37], x[38], x[39], x[40], '14')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
